/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ModuloConfiguracion;

import Modelo.DAL.Grupo_DAO;
import Acceso.Security;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author josmary
 */
public class eliminarGrupoAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    private static final String[] ALLOWED = {"Administrador", "Gestionar Grupos"};

    boolean validarId(ActionErrors errors, String cadena, String err, HttpServletRequest request, Pattern p) {
        Matcher m;
        m = p.matcher(cadena);
        if (!m.matches()) {
            errors.add(err, new ActionMessage("errors.invalid_idGrupo"));
            saveErrors(request, errors);
            return true;
        }
        return false;
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {


        if (!Security.validateSession(request, ALLOWED)) {
            Security.privilegeError(response);
            return mapping.findForward("errorPriv");
        }

        eliminarGrupoForm formulario = (eliminarGrupoForm) form;
        ActionErrors errors = new ActionErrors();
        String id = formulario.getId();
        Pattern p;
        Matcher m;
        boolean hayError = false;
        boolean x = false;

        if (formulario.getId() == null) {
            errors.add("id_vacio", new ActionMessage("errors.GrupoIdVacio"));
            saveErrors(request, errors);
            return mapping.findForward(FAILURE);
        }
        
        if (hayError) {
            formulario.setId(null);
            return mapping.findForward(FAILURE);
        }

        if (!hayError) {

            // Si existe el grupo
            if (Grupo_DAO.comprobarGrupo2(formulario.getId(), formulario.getSede())) {
                if (Grupo_DAO.grupoEnUso(formulario.getId(), formulario.getSede()) == true) {
                    x = (Grupo_DAO.grupoEnUso(formulario.getId(), formulario.getSede()));
                    System.out.println("aaaaaaaaaaaaa" + x);
                    errors.add("No_grupo_puede", new ActionMessage("errors.No_grupo_puede"));
                    saveErrors(request, errors);
                    formulario.setId(null);
                    return mapping.findForward(FAILURE);
                }

                errors.add("grupo_eliminado", new ActionMessage("errors.grupo_eliminado"));
                saveErrors(request, errors);
                formulario.setId(null);
                return mapping.findForward(SUCCESS);

            }

            errors.add("GrupoNo_encontrado", new ActionMessage("errors.GrupoNo_encontrado"));
            saveErrors(request, errors);
            return mapping.findForward(FAILURE);
        }
        return mapping.findForward(SUCCESS);
    }
}
